﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HotelsConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profiles WHERE TravellerName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_firstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_lastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_description.Text = dr("Description")
            End If
        End If
    End Sub

    Protected Sub btn_saveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_saveProfile.Click
        Dim newFileName As String
        Dim SqlString As String
        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        newFileName = myGUID.ToString() + ".jpg"
        If fup_picture.HasFile Then
            Dim picLocationOnServerHardDisk = Request.MapPath("Pictures") & "/" & newFileName
            fup_picture.SaveAs(picLocationOnServerHardDisk)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HotelsConnectionString").ConnectionString)
        If fup_picture.HasFile Then
            SqlString = "UPDATE Profiles SET FirstName=@f1, LastName=@f2, Description=@f3, Picture=@f4 WHERE TravellerName=@f5"
        Else
            SqlString = "UPDATE Profiles SET FirstName=@f1, LastName=@f2, Description=@f3 WHERE TravellerName=@f5"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_description.Text)
        If fup_picture.HasFile Then cmd.Parameters.AddWithValue("@f4", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)

    End Sub
End Class